<?php
class Mroles extends CI_Model{
	protected $_table="tpt_roles";
    protected $_table_2="tpt_role_permissions";
    protected $_table_3="tpt_group_permissions";
    protected $_table_4="tpt_permissions";
	public function __construct(){
		parent::__construct();	
	}
    
	public function listRoles($arrCol){
        $this->db->select(implode(',',$arrCol));
        return $this->db->get($this->_table)->result_array();    
    }

    public function list_all(){
		return $this->db->get($this->_table)->result_array();
    }

    public function insert($data){
        $this->db->insert($this->_table,$data);
    }

    public function check_name($name,$id = ""){
        if($id != ""){
            $this->db->where("id !=",$id);
        }
        $this->db->where("name",$name);
        $query = $this->db->get($this->_table);
        if($query->num_rows() == 0){
            return TRUE;
        }else{
            return FALSE;
        }
    }

    public function get_data($id){
        $this->db->where('id',$id);
        return $this->db->get($this->_table)->row_array();
    }

    public function update($data,$id){
        $this->db->where('id',$id);
        $this->db->update($this->_table,$data);
    }

    public function delete($id){
        $this->db->where('id',$id);
        $this->db->delete($this->_table,$data);
    }

    public function get_privilege($role_id){
        $this->db->select('b.role_id, b.permission_id');
        $this->db->from($this->_table.' as a');
        $this->db->join($this->_table_2.' as b','a.id = b.role_id','left');
        $this->db->where('a.id',$role_id);
        $dataRoleOri = $this->db->get()->result_array();

        $this->db->select('a.id, a.name, b.id as per_id, b.name as per_name, b.order');
        $this->db->from($this->_table_3.' as a');
        $this->db->join($this->_table_4.' as b','a.id = b.group_id','left');
        $this->db->order_by('b.group_id','asc');
        $this->db->order_by('b.order','asc');
        $dataGroupOri = $this->db->get()->result_array();
        $dataPermissions = array();
        if(!empty($dataGroupOri)){
            foreach($dataGroupOri as $itemP){
                $dataPermissions[$itemP['id']]['name'] = $itemP['name'];
                $perType = 'N';
                foreach($dataRoleOri as $itemR){
                    if($itemR['permission_id'] == $itemP['per_id']){
                        $perType = 'Y';
                    }
                    $dataPermissions[$itemP['id']]['permission'][$itemP['order']][$itemP['per_id']] = array(
                        'name' => $itemP['per_name'],
                        'checked' => $perType
                    );
                }
            }
        }
        return $dataPermissions;
    }

    public function update_role_permission($data, $id){
        $this->db->where('role_id',$id);
        $this->db->delete($this->_table_2);

        $dataInsert = array();
        foreach($data as $key => $item){
            $dataInsert[] = array(
                'role_id' => $id,
                'permission_id' => $key 
            );
        }
        $this->db->insert_batch($this->_table_2, $dataInsert); 
    }
}